package com.china08.yxyapi.api.home.kecheng.topic;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLessonChapter;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLessonItem;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonChapterRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;
import com.china08.yxyapi.service.yxyresource.impl.YxyResourceServiceImpl;

/**
 * 专题课程--课时
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengTopicLessonItemSrhController extends YxyApiBaseController {

    // 课时
    @Autowired
    private YxyLessonsTopicLessonItemRepository yxyLessonsTopicLessonItemRepository;

    // 章节
    @Autowired
    private YxyLessonsTopicLessonChapterRepository yxyLessonsTopicLessonChapterRepository;

    /** 视频 */
    @Autowired
    private YxyLessonsVideoRepository yxyLessonsVideoRepository;

    @Autowired
    private YxyResourceServiceImpl yxyResourceServiceImpl;

    @RequestMapping(value = "/api_yw/home/kecheng/topic/lesson/item/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel find(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String lessonItemId = reqModel.lessonItemId;
        YxyLessonsTopicLessonItem yxyLessonsTopicLessonItems = yxyLessonsTopicLessonItemRepository
                .findById(lessonItemId);

        RespModel model = new RespModel();
        if (null != yxyLessonsTopicLessonItems) {
            YxyLessonsTopicLessonChapter topicLessonChapter = yxyLessonsTopicLessonChapterRepository
                    .findById(yxyLessonsTopicLessonItems.getChapterId());

            String weikeId = yxyLessonsTopicLessonItems.getWeikeId();
            if (null != weikeId) {
                ElasticYxyResResource videos = yxyResourceServiceImpl.findById(yxyLessonsTopicLessonItems.getWeikeId());
                if (videos != null) {
                    model.duration = videos.getDuration();
                }
            } else {
                YxyLessonsVideo yxyLessonsVideos = yxyLessonsVideoRepository
                        .findById(yxyLessonsTopicLessonItems.getVideoId());
                if (yxyLessonsVideos != null) {
                    model.duration = yxyLessonsVideos.getDuration();
                }
            }

            model.chapter = topicLessonChapter.getTitle();
            model.title = yxyLessonsTopicLessonItems.getTitle();
            model.intro = yxyLessonsTopicLessonItems.getIntro();
            model.lessonItemTitle = yxyLessonsTopicLessonItems.getTitle();
            model.orderNum = yxyLessonsTopicLessonItems.getOrderNum();
        }

        return model;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiPageReqModel {

        // 课时ID
        public String lessonItemId;
    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 名称
        public String title;
        // 简介
        public String intro;
        // 章节
        public String chapter;
        // 课时名称
        public String lessonItemTitle;
        // 视频时长
        public Integer duration;
        // 课时几
        public Integer orderNum;
    }
}
